﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LX.Web.Excel;

namespace S4Store.Domain.ExhibitionKpi
{

    public class ExhibitionWeekReport : ExhibitionReport
    {
        [Export("执行月度", MergeVertical = true)]
        public string MonthName
        {
            get { return base.Month+"月"; }

        }
        [Export("合计标题1", MergeVertical = true)]
        public override string HjTitle
        {
            get
            {
                return MonthName;
            }
        }

        [Export("执行周", MergeVertical = true)]
        public string WeekName
        {
            get { return "第"+(WeekIndex+1)/2+"周"; }

        }
        [Export("活动主题")]
        public string GXZT
        {
            get { return ExhibitionWeekData.GXZT; }

        }
        [Export("主题活动主题")]
        public string ZTHDZT
        {
            get { return ExhibitionWeekData.ZTHDZT; }

        }
        [Export("主题活动时间")]
        public string ZTHDSJ
        {
            get { return ExhibitionWeekData.ZTHDSJ; }

        }
        [Export("店头活动主题")]
        public string DTHDZT
        {
            get { return ExhibitionWeekData.DTHDZT; }

        }
        [Export("店头活动时间")]
        public string DTHDSJ
        {
            get { return ExhibitionWeekData.DTHDZXSJ; }

        }
        /// <summary>
        /// 广  宣  预  算  资  金  到  位  率
        /// </summary>
        [Export("广宣预算资金到位率", Format = "{0}%", MergeVertical = true)]
        public override decimal GXYSZJDWLV
        {
            get
            {
                //return MonthReport.GXYSZJDWLV;
                return base.GXYSZJDWLV;
            }
        }
        /// <summary>
        /// 广宣计划实施率
        /// </summary>
        [Export("广宣计划实施率", Format = "{0}%", MergeVertical = true)]
        public override decimal GXJHSSLV
        {
            get
            {
                return base.GXJHSSLV;
            }
        }
        /// <summary>
        /// 传  统  广  告  资  金  占  用  率
        /// </summary>
        [Export("传统广告资金占用率", Format = "{0}%", MergeVertical = true)]
        public override decimal CTGGZJZYLV
        {
            get
            {
                return base.CTGGZJZYLV;
            }
        }

        /// <summary>
        /// 传   统  广   告   计  划   执   行   率
        /// </summary>
        [Export("传统广告计划执行率", MergeVertical = true)]
        public override decimal CTGGJHZXLV
        {
            get
            {
                return base.CTGGJHZXLV;
            }
        }
        /// <summary>
        /// 当  月  情  报  收  集  执  行  率
        /// </summary>
        [Export("当月情报收集执行率", Format = "{0}%", MergeVertical = true)]
        public override decimal DYQBSJZXLV
        {
            get
            {
                return base.DYQBSJZXLV;
                //
                //return MonthReport.DYQBSJZXLV;
            }
        }
        /// <summary>
        /// 客户调研分析率
        /// </summary>
        [Export("客户调研分析率", Format = "{0}%", MergeVertical = true)]
        public override decimal KHDYFXLV
        {
            get
            {
                return base.KHDYFXLV;
            }
        }

        /// <summary>
        /// 周主题活动集客贡献度
        /// </summary>
        [Export("主题活动集客贡献度", Format = "{0}%")]
        public override  decimal ZTHDJKGXD
        {
            get
            {
                var c1 = ExhibitionMonthConfig.NBSCLDJDS/4 * RateOfTime / 100;
                if (c1 == 0) return 0;
                return ZTHDXZJKS * 100 / c1;
            }
        }
        public ExhibitionReport MonthReport { get; set; }
        public int WeekIndex { get; set; }
        private List<ExhibitionWeekData> _exhibitionWeekDatas;

        private ExhibitionWeekData ExhibitionWeekData
        {
            get
            {
                if (ExhibitionWeekDatas == null || !ExhibitionWeekDatas.Any())
                {
                    return new ExhibitionWeekData();
                }
                else
                {
                    return ExhibitionWeekDatas[0];
                }
            }
        }

        public override List<ExhibitionWeekData> ExhibitionWeekDatas {
            get
            {
                //int w1 = (WeekIndex - 1) * 2 + 1;
                //int w2 = (WeekIndex - 1) * 2 + 2;
                int w1 = (WeekIndex - 1) + WeekIndex % 2;
                int w2 = (WeekIndex - 1) + WeekIndex % 2+1;

                return MonthReport.ExhibitionWeekDatas.Where(t=>t.WeekIndex==w1||t.WeekIndex==w2).ToList();
            }
            set
            {
                _exhibitionWeekDatas = value;
                if (MonthReport != null)
                {
                    MonthReport.ExhibitionWeekDatas = value;
                }
            }
        }
        public ExhibitionWeekReport(ExhibitionReport monthreport,int weekindex)
        {
            MonthReport = monthreport;
            WeekIndex = weekindex;
            this.Year = monthreport.Year;
            this.Month = monthreport.Month;
            this.Day = monthreport.Day;
            this.ExhibitionKpiStandard = monthreport.ExhibitionKpiStandard;
            this.ExhibitionMonthConfig = monthreport.ExhibitionMonthConfig;
        }
    }

    public class TotalExhibitionReport : ExhibitionReport
    { 
        public TotalExhibitionReport()
            :base()
        {
            
        }
        public override decimal CTGGJKGXD
        {
            get
            {
                var c1 = ExhibitionMonthConfig.NBSCLDJDS * RateOfTime / 100;
                if (c1 == 0) return 0;
                return GGZLDHPC * 100 / c1;
            }
        }
    }

    public class ExhibitionReport : ExportObject
    {
        [Export("合计标题", Colspan = 1)]
        public virtual string HjTitle
        {
            get
            {
                return "合计";
            }
        }
        public ExhibitionReport()
        {
            ExhibitionWeekDatas = new List<ExhibitionWeekData>();
        }

        public int Year { get; set; }
        public int Month { get; set; }
        public int Day { get; set; }
        /// <summary>
        /// 本月执行天数
        /// </summary>
        public int Days
        {
            get { return DateTime.DaysInMonth(Year, Month); }
        }

        /// <summary>
        /// 时间进度
        /// </summary>
        public decimal RateOfTime
        {
            get { return (decimal)Day * 100 / (decimal)Days; }
        }

        public ExhibitionKpiStandard ExhibitionKpiStandard { get; set; }
        public ExhibitionMonthConfig ExhibitionMonthConfig { get; set; }

        public virtual List<ExhibitionWeekData> ExhibitionWeekDatas
        {
            get; 
            set;
            
        }

        /// <summary>
        /// 广宣预算月度分解数
        /// </summary>
        [Export("广宣预算月度分解数")]
        public decimal GXYSYDFJS
        {
            get
            {
                //return ExhibitionMonthConfig.GXYSYDFJS;
                return ExhibitionWeekDatas.Sum(o => o.BZYSYDFJS);
            }
        }

        /// <summary>
        /// 当月广宣资金投入额︵万︶
        /// </summary>
        [Export("广宣资金投入额")]
        public decimal DYGXZJTRE
        {
            get
            {
                return ExhibitionWeekDatas.Sum(t => t.GXZJTRE);
            }
        }
        /// <summary>
        /// 交车数
        /// </summary>
        [Export("交车数")]
        public int JCS
        {
            get { return ExhibitionWeekDatas.Sum(t => t.JCS); }
        }

        /// <summary>
        /// 交车客户面访调研数
        /// </summary>
        [Export("交车客户面访调研数")]
        public int JCKHMTDYS
        {
            get { return ExhibitionWeekDatas.Sum(t => t.JCKHMTDYS); }
        }

        /// <summary>
        /// 单次客户集客成本
        /// </summary>
        [Export("单次客户集客成本",Precision = 3)]
        public decimal DCKHJKCB
        {
            get {
                if (GGZLDHPC == 0) return 0;
                return (decimal)CTGGZJTFE / GGZLDHPC;
            }
        }
        /// <summary>
        /// 客户调研分析率
        /// </summary>
        [Export("客户调研分析率", Format = "{0}%", MergeVertical = true)]
        public virtual decimal KHDYFXLV
        {
            get
            {
                if (JCS == 0) return 0;
                return (decimal)JCKHMTDYS * 100 / JCS;
            }
        }
        /// <summary>
        /// 广宣计划投放数
        /// </summary>
        [Export("广宣计划投放数")]
        public int GXJHTFS {
            get
            {
                //return ExhibitionMonthConfig.GXJHTFS;
                return ExhibitionWeekDatas.Sum(o => o.BZGXJHTFS);
            }
        }

        /// <summary>
        /// 广宣实际投放数
        /// </summary>
        [Export("广宣实际投放数")]
        public int GXSJTFS
        {
            get { return ExhibitionWeekDatas.Sum(t => t.GXSJTFS); }
        }

        /// <summary>
        /// 广宣计划实施率
        /// </summary>
        [Export("广宣计划实施率", Format = "{0}%", MergeVertical = true)]
        public virtual decimal GXJHSSLV
        {
            get
            {
                if (GXJHTFS == 0) return 0;
                return (decimal)GXSJTFS * 100 / GXJHTFS;
            }
        }


        /// <summary>
        /// 传统广告计划投放数
        /// </summary>
        [Export("传统广告计划投放数")]
        public decimal CTGGJHTFS
        {
            get
            {
                //return ExhibitionMonthConfig.CTGGJHTFS;
                return ExhibitionWeekDatas.Sum(o => o.BZCTGGJHTFS);
            }
        }

        /// <summary>
        /// 传   统  广   告   计  划   执   行   率
        /// </summary>
        [Export("传统广告计划执行率", MergeVertical = true)]
        public virtual decimal CTGGJHZXLV
        {
            get
            {
                if (CTGGJHTFS == 0) return 0;
                return CTGGSJTFS * 100 / CTGGJHTFS;
            }
        }

        /// <summary>
        /// 纸媒广告实际投放数
        /// </summary>
        [Export("传统广告实际投放数")]
        public decimal CTGGSJTFS
        {
            get
            {
                return ExhibitionWeekDatas.Sum(t => t.CTGGSJTFS);
            }
        }
        /// <summary>
        /// 纸媒广告资金投放额   ︵  万  ︶ 
        /// </summary>
        [Export("传统广告资金投放额")]
        public decimal CTGGZJTFE
        {
            get { 
                return ExhibitionWeekDatas.Sum(t => t.CTGGZJTFE); 
            }
        }

        /// <summary>
        /// 纸媒广告资金计划投放额   ︵  万  ︶ 
        /// </summary>
        [Export("传统广告资金计划投放额")]
        public decimal CTGGZJJHTFE
        {
            get
            {
                return GXYSYDFJS * this.ExhibitionKpiStandard.CTGGZJZYLV/100;
            }
        }
        /// <summary>
        /// 传  统  广  告  资  金  占  用  率
        /// </summary>
        [Export("传统广告资金占用率", Format = "{0}%", MergeVertical = true)]
        public virtual decimal CTGGZJZYLV
        {
            get
            {
                if (DYGXZJTRE == 0) return 0;
                return (decimal)CTGGZJTFE * 100 / DYGXZJTRE;
            }
        }

        /// <summary>
        /// 广告招徕电话批次
        /// </summary>
        [Export("广告招徕电话批次")]
        public int GGZLDHPC
        {
            get { return ExhibitionWeekDatas.Sum(t => t.GGZLDHPC); }
        }

        /// <summary>
        /// 应时广告招徕电话批次
        /// </summary>
        public int GGZLDHPCOfRate
        {
            get { return (RateOfTime * ExhibitionMonthConfig.NBSCLDJDS * this.ExhibitionKpiStandard.CTGGJKGXD / 10000).AutoRound(); }
        }

        /// <summary>
        /// 传   统   广   告   集   客   贡     献      度
        /// </summary>
        [Export("传统广告集客贡献度", Format = "{0}%")]
        public virtual decimal CTGGJKGXD
        {
            get
            {
                var c1 = ExhibitionMonthConfig.NBSCLDJDS * RateOfTime/100;
                if (c1 == 0) return 0;
                return GGZLDHPC * 100 / ((decimal)c1/(decimal)4);
            }
        }

        /// <summary>
        /// 主题活动新增集客数
        /// </summary>
        [Export("主题活动新增集客数")]
        public int ZTHDXZJKS
        {
            get
            {
                return ExhibitionWeekDatas.Sum(t => t.ZTHDXZJKS);
            }
        }

        /// <summary>
        /// 主题活动目标集客数
        /// </summary>
        [Export("主题活动目标集客数")]
        public int ZTHDMBTXS
        {
            get
            {
                return (this.ExhibitionMonthConfig.NBSCLDJDS * RateOfTime * this.ExhibitionKpiStandard.ZTHDJKGXD / 10000).AutoRound();
            }
        }

        /// <summary>
        /// 主题活动集客贡献度
        /// </summary>
        [Export("主题活动集客贡献度", Format = "{0}%")]
        public virtual decimal ZTHDJKGXD
        {
            get
            {
                var c1 = ExhibitionMonthConfig.NBSCLDJDS * RateOfTime / 100;
                if (c1 == 0) return 0;
                return ZTHDXZJKS * 100 / c1;
            }
        }


        /// <summary>
        /// 主题活动新增集客数差异
        /// </summary>
        [Export("主题活动新增集客数差异")]
        public int ZTHDXZJKSDiff
        {
            get
            {
                var c1 = ExhibitionMonthConfig.NBSCLDJDS * RateOfTime / 100;
                return (int)(ZTHDXZJKS - c1);
            }
        }


        /// <summary>
        /// 计划店头活动数
        /// </summary>
        [Export("计划店头活动数")]
        public int JHDTHDS
        {
            get
            {
                return ExhibitionWeekDatas.Sum(t => t.JHDTHDS);
            }
        }
        /// <summary>
        /// 店头活动实际执行数
        /// </summary>
        [Export("店头活动实际执行数")]
        public decimal DTHDSJZXS
        {
            get
            {
                return ExhibitionWeekDatas.Sum(t => t.DTHDSJZXS);
            }
        }

        /// <summary>
        /// 店头活动策划案成稿数
        /// </summary>
        [Export("店头活动策划案成稿数")]
        public int DTHDCHACGS
        {
            get
            {
                return ExhibitionWeekDatas.Sum(t => t.DTHDCHACGS);
            }
        }

        /// <summary>
        /// 店头活动效果评估报告数
        /// </summary>
        [Export("店头活动效果评估报告数")]
        public int DTHDXGPGBGS
        {
            get
            {
                return ExhibitionWeekDatas.Sum(t => t.DTHDXGPGBGS);
            }
        }
        /// <summary>
        /// 店  头  活    动  计  划  执  行  率
        /// </summary>
        [Export("店头活动计划执行率", Format = "{0}%")]
        public decimal DTHDJHZXLV
        {
            get
            {
                if (JHDTHDS == 0) return 0;
                return (decimal)DTHDSJZXS * 100 / JHDTHDS;
            }
        }

        /// <summary>
        /// 店头活动策划案成稿率
        /// </summary>
        [Export("店头活动策划案成稿率", Format = "{0}%")]
        public decimal DTHDCHCGLV
        {
            get
            {
                if (JHDTHDS == 0) return 0;
                return DTHDCHACGS * 100 / JHDTHDS;
            }
        }

        /// <summary>
        /// 店  头   活  动  效  果  评  估  率
        /// </summary>
        [Export("店头活动效果评估率", Format = "{0}%")]
        public decimal DTHDXGPGLV
        {
            get
            {
                if (DTHDSJZXS == 0) return 0;
                return DTHDXGPGBGS * 100 / DTHDSJZXS;
            }
        }

        /// <summary>
        /// 店头活动目标邀约数
        /// </summary>
        [Export("店头活动目标邀约数")]
        public int DTHDMBYYS
        {
            get
            {
                return ExhibitionWeekDatas.Sum(t => t.DTHDMBYYS);
            }
        }

        /// <summary>
        /// 店头活动成功邀约客户数
        /// </summary>
        [Export("店头活动成功邀约客户数")]
        public int DTHDCGYYS
        {
            get
            {
                return ExhibitionWeekDatas.Sum(t => t.DTHDCGYYS);
            }
        }

        /// <summary>
        /// 店头活动成功邀约客户数差异
        /// </summary>
        [Export("店头活动成功邀约客户数差异")]
        public int DTHDCGYYSDiff
        {
            get
            {
                return DTHDCGYYS - DTHDMBYYS;
            }
        }

        /// <summary>
        /// 店头活动邀约成功率
        /// </summary>
        [Export("店头活动邀约成功率", Format = "{0}%")]
        public decimal ZTHDYYCGLV
        {
            get
            {
                if (DTHDMBYYS == 0) return 0;
                return DTHDCGYYS * 100 / DTHDMBYYS;
            }
        }
        /// <summary>
        /// 店头活动战败客二次消化数
        /// </summary>
        [Export("店头活动战败客二次消化数")]
        public int DTHDZBKECXHS
        {
            get
            {
                return ExhibitionWeekDatas.Sum(t => t.DTHDZBKECXHS);
            }
        }
        /// <summary>
        /// 店头活动战败客二次消化率
        /// </summary>
        [Export("店头活动战败客二次消化率", Format = "{0}%")]
        public decimal DTHDZBKECXHLV
        {
            get
            {
                if (DTHDCGYYS == 0) return 0;
                return DTHDZBKECXHS*100 / DTHDCGYYS;
            }
        }
        /// <summary>
        /// 店头活动投入资金
        /// </summary>
        [Export("店头活动投入资金")]
        public decimal DTHDTRZJ
        {
            get
            {
                return ExhibitionWeekDatas.Sum(t => t.DTHDTRZJ);
            }
        }
        /// <summary>
        /// 店头    活     动    单     次     客     户     集     客     成     本      ︵    万    ︶
        /// </summary>
        [Export("店头活动单次客户集客成本", Precision = 3)]
        public decimal DTHDDCKHJKCB
        {
            get
            {
                if (DTHDCGYYS == 0) return 0;
                return DTHDTRZJ / DTHDCGYYS;
            }
        }

        /// <summary>
        /// 计划主题活动数
        /// </summary>
        [Export("计划主题活动数")]
        public int JHZTHDS
        {
            get
            {
                return ExhibitionWeekDatas.Sum(t => t.JHZTHDS);
            }
        }

        /// <summary>
        /// 主题活动实际执行数
        /// </summary>
        [Export("主题活动实际执行数")]
        public decimal ZTHDSJZXS
        {
            get
            {
                return ExhibitionWeekDatas.Sum(t => t.ZTHDSJZXS);
            }
        }

        /// <summary>
        /// 主题活动策划案成稿数
        /// </summary>
        [Export("主题活动策划案成稿数")]
        public int ZTHDCHACGS
        {
            get
            {
                return ExhibitionWeekDatas.Sum(t => t.ZTHDCHACGS);
            }
        }

        /// <summary>
        /// 主题活动效果评估报告数
        /// </summary>
        [Export("主题活动效果评估报告数")]
        public int ZTHDXGPGBGS
        {
            get
            {
                return ExhibitionWeekDatas.Sum(t => t.ZTHDXGPGBGS);
            }
        }

        /// <summary>
        /// 主题活动计划执行率
        /// </summary>
        [Export("主题活动计划执行率", Format = "{0}%")]
        public decimal ZTHDJHZXLV
        {
            get
            {
                if (JHZTHDS == 0) return 0;
                return ZTHDSJZXS * 100 / JHZTHDS;
            }
        }

        /// <summary>
        /// 主题活动策划案成稿率
        /// </summary>
        [Export("主题活动策划案成稿率", Format = "{0}%")]
        public decimal ZTHDCHCGLV
        {
            get
            {
                if (JHZTHDS == 0) return 0;
                return ZTHDCHACGS * 100 / JHZTHDS;
            }
        }

   
        /// <summary>
        /// 主题活动效果评估率
        /// </summary>
        [Export("主题活动效果评估率", Format = "{0}%")]
        public decimal ZTHDXGPGLV
        {
            get
            {
                if (ZTHDSJZXS == 0) return 0;
                return (decimal)ZTHDXGPGBGS * 100 / ZTHDSJZXS;
            }
        }

        /// <summary>
        /// 主题活动投入资金
        /// </summary>
        [Export("主题活动投入资金")]
        public decimal ZTHDTRZJ
        {
            get
            {
                return ExhibitionWeekDatas.Sum(t => t.ZTHDTRZJ);
            }
        }

        /// <summary>
        /// 计划市场情报搜集次数
        /// </summary>
        [Export("计划市场情报搜集次数")]
        public int JHSCQBSJCS
        {
            get
            {
                return ExhibitionWeekDatas.Sum(t => t.JHSCQBSJCS);
            }
        }
        /// <summary>
        /// 市场情报实际搜集报告数
        /// </summary>
        [Export("市场情报实际搜集报告数")]
        public int CSQBSJSJBGS
        {
            get
            {
                return ExhibitionWeekDatas.Sum(t => t.CSQBSJSJBGS);
            }
        }
        /// <summary>
        /// 主     题    活     动    单     次     客     户     集     客     成     本      ︵    万    ︶
        /// </summary>
        [Export("主题活动单次客户集客成本",Precision = 3)]
        public decimal ZTHDDCKHJKCB
        {
            get
            {
                if (ZTHDXZJKS == 0) return 0;
                return ZTHDTRZJ  / ZTHDXZJKS;
            }
        }
        /// <summary>
        /// 当  月  情  报  收  集  执  行  率
        /// </summary>
        [Export("当月情报收集执行率", Format = "{0}%", MergeVertical = true)]
        public virtual decimal DYQBSJZXLV
        {
            get
            {
                if (JHSCQBSJCS == 0) return 0;
                return CSQBSJSJBGS*100 / JHSCQBSJCS;
            }
        }

        /// <summary>
        /// 广  宣  预  算  资  金  到  位  率
        /// </summary>
        [Export("广宣预算资金到位率", Format = "{0}%", MergeVertical = true)]
        public virtual decimal GXYSZJDWLV
        {
            get
            {
                if (GXYSYDFJS == 0) return 0;
                return DYGXZJTRE * 100 / GXYSYDFJS;
            }
        }
    }
}
